package xscjglxt;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Tools {
    //平均分
    public static double getAvg(List<Student> list) {
        //所有成绩的和
        double sum = 0;
        Iterator<Student> it = list.iterator();
        while (it.hasNext()) {
            Student stu = it.next();
            sum += stu.getScore();
        }
        return sum / list.size();
    }

    //最高分学生信息
    public static Student getMax(List<Student> list) {
        Iterator<Student> it = list.iterator();
        Student stuMax = it.next();
        if (it.hasNext()) {
            Student stu = it.next();
            if (it.next().getScore() > stuMax.getScore()) {
                stuMax = stu;
            }
        }
        return stuMax;
    }

    //介于x 和y分数区间的学生
    public static List<Student> getBelow60(List<Student> list, double x, double y) {
        List<Student> stus = new ArrayList<>();
        for (Student stu : list) {
            if (stu.getScore() >= x && stu.getScore() < y) {
                stus.add(stu);
            }
        }
        return stus;
    }

    public static class Exec {
        static List<Student> list = new ArrayList<>();

        public static void main(String[] args) {
            init();
            System.out.println("平均分是" + Tools.getAvg(list));
            Student stu = Tools.getMax(list);
            System.out.println(stu);
            List<Student> b160 = Tools.getBelow60(list, 60, 70);
            System.out.println(list);
        }

        public static void init() {
            Student stu = new Student("张三", "1001", 60);
            Student stu2 = new Student("李四", "1082", 80);
            Student stu3 = new Student("王五", "1003", 50);
            Student stu4 = new Student("马六", "1004", 90);
            Student stu5 = new Student("朱七七", "1005", 40);
            Student stu6 = new Student("猪八戒", "1006", 70);
            Student stu7 = new Student("美丽", "1007", 28);
            Collections.addAll(list, stu, stu2, stu3, stu4, stu5, stu6, stu7);
        }
    }
}

